<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Library\Rms\RmsShopApi;
use App\Models\Common\OperateLogModel;
use App\Models\Common\ResourcesModel;
use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseCheckRectifyModel;
use App\Models\Cruise\CruiseCheckRectifyReplyModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\BaseService;
use App\Service\Cruise\Check\CruiseCheckRectifyService;
use App\Service\Cruise\Check\CruiseCheckReportService;
use App\Service\Cruise\Check\CruiseOrderService;
use App\Service\Cruise\Merchant\CruiseCheckService;
use App\Service\Cruise\Mission\CheckInputService;
use App\Service\Cruise\Shop\CruiseShopService;
use App\Support\CustomForm\AttributeCruiseSupport;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class CruiseCheckRectifyController extends BaseController
{
    public function lists(Request $request): JsonResponse
    {
        $pageNum   = (int)$request->get('pageNum');
        $pageSize  = (int)$request->get('pageSize');
        $status    = (int)$request->get('status', CruiseCheckRectifyModel::STATUS_WAIT_TOTAL);
        $shopInfo  = $request->get('shopId');
        $startDate = $request->get('startDate');
        $endDate   = $request->get('endDate');
        $dealUid   = $request->get('dealUid', 0);
        $cruiseCheckDetailId = $request->get('cruiseCheckDetailId', 0);
        $templateType = (int)$request->get('templateType', CruiseTemplateModel::TYPE_3);
        $cruiseCheckCompanyIds = (string)$request->get('cruiseCheckCompanyIds');
        $cruiseCheckCompanyIds = $cruiseCheckCompanyIds ? explode(',', $cruiseCheckCompanyIds) : [];
        $shopIds = $request->get('shopIds','');
        $shopIds = $shopIds ? explode(',',$shopIds) : [];
        $submitStartDate = $request->get('submitStartDate','');
        $submitEndDate   = $request->get('submitEndDate','');

        $this->validate($request, [
            'status'    => 'integer',
            'pageNum'   => 'integer',
            'pageSize'  => 'integer',
            'startDate' => 'date',
            'endDate'   => 'date',
            'submitStartDate' => 'date',
            'submitEndDate'   => 'date',
        ]);

        $submitStartTime = $submitEndTime = $startTime = $endTime = 0;
        if ($startDate) {
            $startTime = Carbon::parse($startDate)->startOfDay()->timestamp;
        }

        if ($endDate) {
            $endTime = Carbon::parse($endDate)->endOfDay()->timestamp;
        }

        if ($submitStartDate){
            $submitStartTime = Carbon::parse($submitStartDate)->startOfDay()->timestamp;
        }

        if($submitEndDate){
            $submitEndTime = Carbon::parse($submitEndDate)->endOfDay()->timestamp;
        }

        $currentUid = getRmsUserId();

        return $this->success(
            (new CruiseCheckRectifyService(0, $currentUid))->lists($shopInfo, $status, $pageSize, $pageNum, $startTime,
                $endTime, 0, $templateType, $cruiseCheckCompanyIds, $dealUid, $cruiseCheckDetailId,$shopIds,$submitStartTime,$submitEndTime)
        );
    }

    public function audit(Request $request, int $id): JsonResponse
    {
        $auditResult = (int)$request->post('auditResult');
        $auditText   = (string)$request->post('auditText');

        $this->validate($request, [
            'auditResult' => [
                'required',
                'integer',
                Rule::in([CruiseCheckRectifyModel::AUDIT_RESULT_OK, CruiseCheckRectifyModel::AUDIT_RESULT_FAIL])
            ],
            'auditText'   => 'required|string',
        ]);

        $currentUid = getRmsUserId();

        (new CruiseCheckRectifyService($id, $currentUid))->auditReply($auditResult, $auditText);

        return $this->success();
    }

    /**
     * 待整改批量关闭
     * @param Request $request
     * @return JsonResponse
     */
    public function batchClose(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
            'ids' => 'required|array',
        ]);

        $currentUid = getRmsUserId();
        (new CruiseCheckRectifyService(0, $currentUid))->batchClose($request->ids);
        return $this->success();
    }
}
